package com.wnnight.wys.web.role;

import com.wnnight.wys.entity.role.MenuPerm;
import com.wnnight.wys.security.OaUser;
import com.wnnight.wys.service.role.MenuPermService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Controller("portalIndexController")
@RequestMapping("/api/v1/wy")
public class IndexController {
    @Autowired
    private MenuPermService menuPermService;
    @RequestMapping ("/")
    public String index(Model model, HttpSession session) {
        Object o = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        log.debug("Pricinple：{}", o);
        OaUser user=(OaUser) o;
        //User user = (User) session.getAttribute("user");
        int roleId = user.getRoleId();
        List<MenuPerm> userMenus = menuPermService.getByRoleId(roleId);
//        List<MenuPerm> menus = new ArrayList<>();
//        for (Menu userMenu : userMenus) {
//            if (userMenu.getParentMenu() == 0) {
//                menus.add(userMenu);
//                List<Menu> sonMenus = new ArrayList<>();
//                for (Menu subMenu : userMenus) {
//                    if (userMenu.getId() == subMenu.getParentMenu()) {
//                        sonMenus.add(subMenu);
//                    }
//                }
//                userMenu.setSubMenus(sonMenus);
//            }
//        }
        model.addAttribute("userAccount", user.getId());
        model.addAttribute("menus", userMenus);
        return "redirect:/index";
    }
}
